package huas.ant.controller;

import huas.ant.common.PageResult;
import huas.ant.common.Result;
import huas.ant.entity.Lesson;
import huas.ant.service.LessonService;
import huas.ant.vo.LessonVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/lesson")
public class LessonController {
    @Autowired
    private LessonService lessonService;

    /**
     * 分页列表查询
     */
    @GetMapping("/page")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("传递的分页参数为：{},{}",page,pageSize);
        PageResult pageResult = lessonService.page(page,pageSize);
        return Result.success(pageResult);
    }
    /**
     * 添加课程
     */
    @PostMapping
    public Result insert(@RequestBody Lesson lesson){
        log.info("添加的课程信息为：{}",lesson);
        lessonService.insert(lesson);
        return Result.success();
    }

    /**
     * 修改课程
     */
    @PutMapping
    public Result update(@RequestBody Lesson lesson){
        log.info("要修改的课程信息为：{}",lesson);
        lessonService.update(lesson);
        return Result.success();
    }
    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("要删除的课程id为：{}",id);
        lessonService.delete(id);
        return Result.success();
    }

    /**
     * 回写课程的详细信息
     */
    @GetMapping("/{id}")
    public Result<LessonVO> selectDetails(@PathVariable Integer id){
        log.info("要查看详细信息的课程id为：{}",id);
        LessonVO lessonVO = lessonService.selectDetails(id);
        log.info("回写数据:{}",lessonVO);
        return Result.success(lessonVO);
    }

}
